/**
 * 
 */
package cn.salesuite.movie.comparator;

import java.util.Comparator;

import cn.salesuite.movie.domain.CinemaPlan;

/**
 * @author Tony Shen
 *
 */
public class CinemaPlanComparator implements Comparator<CinemaPlan> {

	@Override
	public int compare(CinemaPlan object1, CinemaPlan object2) {
		String playTime1 = (object1).playTime;
		String playHour1 = playTime1.substring(0, 2);
		String playMinute1 = playTime1.substring(3, 5);
		int hour1 = getHour(playHour1);
		
		String playTime2 = (object2).playTime;
		String playHour2 = playTime2.substring(0, 2);
		String playMinute2 = playTime2.substring(3, 5);
		int hour2 = getHour(playHour2);
		
		if (hour1 > hour2) {
			return 1;
		} else if (hour1 == hour2) {
			int minute1 = getMinute(playMinute1);
			int minute2 = getMinute(playMinute2);
			if (minute1 > minute2) {
				return 1;
			} else {
				return -1;
			}
			
		} 
		return -1;
	}
	
	private int getHour(String playHour) {
		int hour = 0;
		if (playHour.startsWith("0")) {
			hour = Integer.parseInt(playHour.substring(1,2));
		} else {
			hour = Integer.parseInt(playHour);
		}
		return hour;
	}
	
	private int getMinute(String playMinute) {
		int minute = 0;
		if (playMinute.startsWith("0")) {
			minute = Integer.parseInt(playMinute.substring(1,2));
		} else {
			minute = Integer.parseInt(playMinute);
		}
		return minute;
	}

}
